package my.utils;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

public class DBUtils {
	private static String user = "owx";
	private static String password = "ouweixuan785";
	private static String url = "jdbc:oracle:thin://localhost:1521/orcl";
	private static String dirver = "oracle.jdbc.driver.OracleDriver";
	
	/**
	 * 用于打开数据库连接
	 * @return Connection conn
	 */
	public static Connection getConnection(){
		Connection conn = null;
		
		try {
			Class.forName(dirver);
			conn = DriverManager.getConnection(url,user,password);
			
		} catch (ClassNotFoundException e) {
			System.out.println("未找到驱动类,请检查设置或联系管理员");
			e.printStackTrace();
		}catch (SQLException e1) {
			System.out.println("数据库连接失败,请检查数据库参数或查看服务是否已打开或联系管理员");
			e1.printStackTrace();
		}
		return conn;
	}
	
	public static void close(Connection conn,Statement stmt ,ResultSet rs){
		if(rs != null){
			try {
				rs.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
		if(stmt != null){
			try {
				stmt.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
		if(conn != null){
			try {
				conn.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
	}
	
}
